package com.github.thorqin.toolkit.utility;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by nuo.qin on 12/25/2014.
 */
public final class MimeUtils {
    public static final Map<String, String> MIME;
    public static final Map<String, String> MIME_SUFFIX;

    /**
     * Unknown mime type
     */
    public static final String UNKNOWN_MIME = "application/octet-stream";
    static {
        MIME = new HashMap<>();
        MIME.put("323", "text/h323");
		MIME.put("3gp", "video/3gpp");
        MIME.put("acx", "application/internet-property-stream");
        MIME.put("ai", "application/postscript");
        MIME.put("aif", "audio/x-aiff");
        MIME.put("aifc", "audio/x-aiff");
        MIME.put("aiff", "audio/x-aiff");
        MIME.put("apk", "application/vnd.android.package-archive");
        MIME.put("asf", "video/x-ms-asf");
        MIME.put("asr", "video/x-ms-asf");
        MIME.put("asx", "video/x-ms-asf");
        MIME.put("au", "audio/basic");
        MIME.put("avi", "video/x-msvideo");
        MIME.put("axs", "application/olescript");
        MIME.put("bas", "text/plain");
        MIME.put("bcpio", "application/x-bcpio");
        MIME.put("bin", "application/octet-stream");
        MIME.put("bmp", "image/bmp");
        MIME.put("c", "text/plain");
        MIME.put("cat", "application/vnd.ms-pkiseccat");
        MIME.put("cc", "text/plain");
        MIME.put("cdf", "application/x-cdf");
        MIME.put("cer", "application/x-x509-ca-cert");
        MIME.put("class", "application/octet-stream");
        MIME.put("cpp", "text/plain");
        MIME.put("clp", "application/x-msclip");
        MIME.put("cmx", "image/x-cmx");
        MIME.put("cod", "image/cis-cod");
        MIME.put("cpio", "application/x-cpio");
        MIME.put("crd", "application/x-mscardfile");
        MIME.put("crl", "application/pkix-crl");
        MIME.put("crt", "application/x-x509-ca-cert");
        MIME.put("csh", "application/x-csh");
        MIME.put("cs", "text/plain");
        MIME.put("css", "text/css");
        MIME.put("dcr", "application/x-director");
        MIME.put("der", "application/x-x509-ca-cert");
        MIME.put("dir", "application/x-director");
        MIME.put("dll", "application/x-msdownload");
        MIME.put("dms", "application/octet-stream");
        MIME.put("doc", "application/vnd.ms-word");
        MIME.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        MIME.put("docm", "application/vnd.ms-word.document.macroEnabled.12");
        MIME.put("dot", "application/msword");
        MIME.put("dvi", "application/x-dvi");
        MIME.put("dxr", "application/x-director");
        MIME.put("emf", "image/x-emf");
        MIME.put("eps", "application/postscript");
        MIME.put("etx", "text/x-setext");
        MIME.put("evy", "application/envoy");
        MIME.put("exe", "application/octet-stream");
        MIME.put("fif", "application/fractals");
        MIME.put("flr", "x-world/x-vrml");
        MIME.put("gif", "image/gif");
        MIME.put("gtar", "application/x-gtar");
        MIME.put("gz", "application/x-gzip");
        MIME.put("h", "text/plain");
        MIME.put("hdf", "application/x-hdf");
        MIME.put("hlp", "application/winhlp");
        MIME.put("hqx", "application/mac-binhex40");
        MIME.put("hta", "application/hta");
        MIME.put("htc", "text/x-component");
        MIME.put("htm", "text/html");
        MIME.put("html", "text/html");
        MIME.put("htt", "text/webviewhtml");
        MIME.put("ico", "image/x-icon");
        MIME.put("ief", "image/ief");
        MIME.put("iii", "application/x-iphone");
        MIME.put("ins", "application/x-internet-signup");
        MIME.put("ipa", "application/octet-stream");
        MIME.put("isp", "application/x-internet-signup");
        MIME.put("jar", "application/java-archive");
        MIME.put("jfif", "image/pipeg");
        MIME.put("java", "text/plain");
        MIME.put("jpe", "image/jpeg");
        MIME.put("jpeg", "image/jpeg");
        MIME.put("jpg", "image/jpeg");
        MIME.put("js", "application/x-javascript");
        MIME.put("json", "application/json");
        MIME.put("latex", "application/x-latex");
        MIME.put("lha", "application/octet-stream");
        MIME.put("lsf", "video/x-la-asf");
        MIME.put("lsx", "video/x-la-asf");
        MIME.put("lzh", "application/octet-stream");
        MIME.put("m13", "application/x-msmediaview");
        MIME.put("m14", "application/x-msmediaview");
        MIME.put("m3u", "audio/x-mpegurl");
        MIME.put("man", "application/x-troff-man");
        MIME.put("mdb", "application/x-msaccess");
        MIME.put("me", "application/x-troff-me");
        MIME.put("mht", "message/rfc822");
        MIME.put("mhtml", "message/rfc822");
        MIME.put("mid", "audio/mid");
        MIME.put("mny", "application/x-msmoney");
        MIME.put("mov", "video/quicktime");
        MIME.put("movie", "video/x-sgi-movie");
        MIME.put("mp2", "video/mpeg");
        MIME.put("mp3", "audio/mpeg");
		MIME.put("mp4", "video/mp4");
		MIME.put("m4v", "video/x-m4v");
        MIME.put("mpa", "video/mpeg");
        MIME.put("mpe", "video/mpeg");
        MIME.put("mpeg", "video/mpeg");
        MIME.put("mpg", "video/mpeg");
        MIME.put("mpp", "application/vnd.ms-project");
        MIME.put("mpv2", "video/mpeg");
        MIME.put("ms", "application/x-troff-ms");
        MIME.put("mvb", "application/x-msmediaview");
        MIME.put("nws", "message/rfc822");
        MIME.put("oda", "application/oda");
        MIME.put("p10", "application/pkcs10");
        MIME.put("p12", "application/x-pkcs12");
        MIME.put("p7b", "application/x-pkcs7-certificates");
        MIME.put("p7c", "application/x-pkcs7-mime");
        MIME.put("p7m", "application/x-pkcs7-mime");
        MIME.put("p7r", "application/x-pkcs7-certreqresp");
        MIME.put("p7s", "application/x-pkcs7-signature");
        MIME.put("pbm", "image/x-portable-bitmap");
        MIME.put("pdf", "application/pdf");
        MIME.put("pfx", "application/x-pkcs12");
        MIME.put("pgm", "image/x-portable-graymap");
        MIME.put("pko", "application/ynd.ms-pkipko");
        MIME.put("plist", "text/xml");
        MIME.put("pma", "application/x-perfmon");
        MIME.put("pmc", "application/x-perfmon");
        MIME.put("pml", "application/x-perfmon");
        MIME.put("pmr", "application/x-perfmon");
        MIME.put("pmw", "application/x-perfmon");
        MIME.put("pnm", "image/x-portable-anymap");
        MIME.put("png", "image/png");
        MIME.put("pot,", "application/vnd.ms-powerpoint");
        MIME.put("ppm", "image/x-portable-pixmap");
        MIME.put("pps", "application/vnd.ms-powerpoint");
        MIME.put("ppt", "application/vnd.ms-powerpoint");
        MIME.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        MIME.put("ppat", "application/vnd.ms-powerpoint.presentation.macroEnabled.12");
        MIME.put("prf", "application/pics-rules");
        MIME.put("ps", "application/postscript");
        MIME.put("pub", "application/x-mspublisher");
        MIME.put("qt", "video/quicktime");
        MIME.put("ra", "audio/x-pn-realaudio");
        MIME.put("ram", "audio/x-pn-realaudio");
        MIME.put("ras", "image/x-cmu-raster");
        MIME.put("rgb", "image/x-rgb");
        MIME.put("rmi", "audio/mid");
        MIME.put("roff", "application/x-troff");
        MIME.put("rtf", "application/rtf");
        MIME.put("rtx", "text/richtext");
        MIME.put("scd", "application/x-msschedule");
        MIME.put("sct", "text/scriptlet");
        MIME.put("setpay", "application/set-payment-initiation");
        MIME.put("setreg", "application/set-registration-initiation");
        MIME.put("sh", "application/x-sh");
        MIME.put("shar", "application/x-shar");
        MIME.put("sit", "application/x-stuffit");
        MIME.put("snd", "audio/basic");
        MIME.put("spc", "application/x-pkcs7-certificates");
        MIME.put("spl", "application/futuresplash");
        MIME.put("src", "application/x-wais-source");
        MIME.put("sst", "application/vnd.ms-pkicertstore");
        MIME.put("stl", "application/vnd.ms-pkistl");
        MIME.put("stm", "text/html");
        MIME.put("svg", "image/svg+xml");
        MIME.put("sv4cpio", "application/x-sv4cpio");
        MIME.put("sv4crc", "application/x-sv4crc");
        MIME.put("swf", "application/x-shockwave-flash");
        MIME.put("t", "application/x-troff");
        MIME.put("tar", "application/x-tar");
        MIME.put("tcl", "application/x-tcl");
        MIME.put("tex", "application/x-tex");
        MIME.put("texi", "application/x-texinfo");
        MIME.put("texinfo", "application/x-texinfo");
        MIME.put("tgz", "application/x-compressed");
        MIME.put("tif", "image/tiff");
        MIME.put("tiff", "image/tiff");
        MIME.put("tr", "application/x-troff");
        MIME.put("trm", "application/x-msterminal");
        MIME.put("tsv", "text/tab-separated-values");
        MIME.put("txt", "text/plain");
        MIME.put("uls", "text/iuls");
        MIME.put("ustar", "application/x-ustar");
        MIME.put("vcf", "text/x-vcard");
        MIME.put("vrml", "x-world/x-vrml");
        MIME.put("wav", "audio/x-wav");
        MIME.put("wcm", "application/vnd.ms-works");
        MIME.put("wdb", "application/vnd.ms-works");
        MIME.put("wks", "application/vnd.ms-works");
        MIME.put("wmf", "application/x-msmetafile");
        MIME.put("wps", "application/vnd.ms-works");
        MIME.put("wri", "application/x-mswrite");
        MIME.put("wrl", "x-world/x-vrml");
        MIME.put("wrz", "x-world/x-vrml");
        MIME.put("xaf", "x-world/x-vrml");
        MIME.put("xbm", "image/x-xbitmap");
        MIME.put("xhtml", "application/xhtml+xml");
        MIME.put("xla", "application/vnd.ms-excel");
        MIME.put("xlc", "application/vnd.ms-excel");
        MIME.put("xlm", "application/vnd.ms-excel");
        MIME.put("xls", "application/vnd.ms-excel");
        MIME.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        MIME.put("xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12");
        MIME.put("xlt", "application/vnd.ms-excel");
        MIME.put("xlw", "application/vnd.ms-excel");
        MIME.put("xml", "text/xml");
        MIME.put("xof", "x-world/x-vrml");
        MIME.put("xpm", "image/x-xpixmap");
        MIME.put("xwd", "image/x-xwindowdump");
        MIME.put("z", "application/x-compress");
        MIME.put("zip", "application/zip");

        MIME_SUFFIX = new HashMap<>();
        MIME_SUFFIX.put("application/envoy", "evy");
        MIME_SUFFIX.put("application/fractals", "fif");
        MIME_SUFFIX.put("application/futuresplash", "spl");
        MIME_SUFFIX.put("application/hta", "hta");
        MIME_SUFFIX.put("application/internet-property-stream", "acx");
        MIME_SUFFIX.put("application/java-archive", "jar");
        MIME_SUFFIX.put("application/json", "json");
        MIME_SUFFIX.put("application/mac-binhex40", "hqx");
        MIME_SUFFIX.put("application/msword", "dot");
        MIME_SUFFIX.put("application/octet-stream", "bin");
        MIME_SUFFIX.put("application/oda", "oda");
        MIME_SUFFIX.put("application/olescript", "axs");
        MIME_SUFFIX.put("application/pdf", "pdf");
        MIME_SUFFIX.put("application/pics-rules", "prf");
        MIME_SUFFIX.put("application/pkcs10", "p10");
        MIME_SUFFIX.put("application/pkix-crl", "crl");
        MIME_SUFFIX.put("application/postscript", "ai");
        MIME_SUFFIX.put("application/rtf", "rtf");
        MIME_SUFFIX.put("application/set-payment-initiation", "setpay");
        MIME_SUFFIX.put("application/set-registration-initiation", "setreg");
        MIME_SUFFIX.put("application/vnd.android.package-archive", "apk");
        MIME_SUFFIX.put("application/vnd.ms-excel", "xls");
        MIME_SUFFIX.put("application/vnd.ms-excel.sheet.macroEnabled.12", "xlsm");
        MIME_SUFFIX.put("application/vnd.ms-pkicertstore", "sst");
        MIME_SUFFIX.put("application/vnd.ms-pkiseccat", "cat");
        MIME_SUFFIX.put("application/vnd.ms-pkistl", "stl");
        MIME_SUFFIX.put("application/vnd.ms-powerpoint", "ppt");
        MIME_SUFFIX.put("application/vnd.ms-powerpoint.presentation.macroEnabled.12", "ppat");
        MIME_SUFFIX.put("application/vnd.ms-project", "mpp");
        MIME_SUFFIX.put("application/vnd.ms-word", "doc");
        MIME_SUFFIX.put("application/vnd.ms-word.document.macroEnabled.12", "docm");
        MIME_SUFFIX.put("application/vnd.ms-works", "wcm");
        MIME_SUFFIX.put("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx");
        MIME_SUFFIX.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
        MIME_SUFFIX.put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
        MIME_SUFFIX.put("application/winhlp", "hlp");
        MIME_SUFFIX.put("application/x-bcpio", "bcpio");
        MIME_SUFFIX.put("application/x-cdf", "cdf");
        MIME_SUFFIX.put("application/x-compress", "z");
        MIME_SUFFIX.put("application/x-compressed", "tgz");
        MIME_SUFFIX.put("application/x-cpio", "cpio");
        MIME_SUFFIX.put("application/x-csh", "csh");
        MIME_SUFFIX.put("application/x-director", "dir");
        MIME_SUFFIX.put("application/x-dvi", "dvi");
        MIME_SUFFIX.put("application/x-gtar", "gtar");
        MIME_SUFFIX.put("application/x-gzip", "gz");
        MIME_SUFFIX.put("application/x-hdf", "hdf");
        MIME_SUFFIX.put("application/x-internet-signup", "isp");
        MIME_SUFFIX.put("application/x-iphone", "iii");
        MIME_SUFFIX.put("application/x-javascript", "js");
        MIME_SUFFIX.put("application/x-latex", "latex");
        MIME_SUFFIX.put("application/x-msaccess", "mdb");
        MIME_SUFFIX.put("application/x-mscardfile", "crd");
        MIME_SUFFIX.put("application/x-msclip", "clp");
        MIME_SUFFIX.put("application/x-msdownload", "dll");
        MIME_SUFFIX.put("application/x-msmediaview", "mvb");
        MIME_SUFFIX.put("application/x-msmetafile", "wmf");
        MIME_SUFFIX.put("application/x-msmoney", "mny");
        MIME_SUFFIX.put("application/x-mspublisher", "pub");
        MIME_SUFFIX.put("application/x-msschedule", "scd");
        MIME_SUFFIX.put("application/x-msterminal", "trm");
        MIME_SUFFIX.put("application/x-mswrite", "wri");
        MIME_SUFFIX.put("application/x-perfmon", "pma");
        MIME_SUFFIX.put("application/x-pkcs12", "p12");
        MIME_SUFFIX.put("application/x-pkcs7-certificates", "p7b");
        MIME_SUFFIX.put("application/x-pkcs7-certreqresp", "p7r");
        MIME_SUFFIX.put("application/x-pkcs7-mime", "p7m");
        MIME_SUFFIX.put("application/x-pkcs7-signature", "p7s");
        MIME_SUFFIX.put("application/x-sh", "sh");
        MIME_SUFFIX.put("application/x-shar", "shar");
        MIME_SUFFIX.put("application/x-shockwave-flash", "swf");
        MIME_SUFFIX.put("application/x-stuffit", "sit");
        MIME_SUFFIX.put("application/x-sv4cpio", "sv4cpio");
        MIME_SUFFIX.put("application/x-sv4crc", "sv4crc");
        MIME_SUFFIX.put("application/x-tar", "tar");
        MIME_SUFFIX.put("application/x-tcl", "tcl");
        MIME_SUFFIX.put("application/x-tex", "tex");
        MIME_SUFFIX.put("application/x-texinfo", "texi");
        MIME_SUFFIX.put("application/x-troff", "roff");
        MIME_SUFFIX.put("application/x-troff-man", "man");
        MIME_SUFFIX.put("application/x-troff-me", "me");
        MIME_SUFFIX.put("application/x-troff-ms", "ms");
        MIME_SUFFIX.put("application/x-ustar", "ustar");
        MIME_SUFFIX.put("application/x-wais-source", "src");
        MIME_SUFFIX.put("application/x-x509-ca-cert", "crt");
        MIME_SUFFIX.put("application/xhtml+xml", "xhtml");
        MIME_SUFFIX.put("application/ynd.ms-pkipko", "pko");
        MIME_SUFFIX.put("application/zip", "zip");
        MIME_SUFFIX.put("audio/basic", "au");
        MIME_SUFFIX.put("audio/mid", "mid");
        MIME_SUFFIX.put("audio/mpeg", "mp3");
        MIME_SUFFIX.put("audio/x-aiff", "aif");
        MIME_SUFFIX.put("audio/x-mpegurl", "m3u");
        MIME_SUFFIX.put("audio/x-pn-realaudio", "ra");
        MIME_SUFFIX.put("audio/x-wav", "wav");
        MIME_SUFFIX.put("image/bmp", "bmp");
        MIME_SUFFIX.put("image/cis-cod", "cod");
        MIME_SUFFIX.put("image/gif", "gif");
        MIME_SUFFIX.put("image/ief", "ief");
        MIME_SUFFIX.put("image/jpeg", "jpeg");
        MIME_SUFFIX.put("image/pipeg", "jfif");
        MIME_SUFFIX.put("image/png", "png");
        MIME_SUFFIX.put("image/svg+xml", "svg");
        MIME_SUFFIX.put("image/tiff", "tiff");
        MIME_SUFFIX.put("image/x-cmu-raster", "ras");
        MIME_SUFFIX.put("image/x-cmx", "cmx");
        MIME_SUFFIX.put("image/x-emf", "emf");
        MIME_SUFFIX.put("image/x-icon", "ico");
        MIME_SUFFIX.put("image/x-portable-anymap", "pnm");
        MIME_SUFFIX.put("image/x-portable-bitmap", "pbm");
        MIME_SUFFIX.put("image/x-portable-graymap", "pgm");
        MIME_SUFFIX.put("image/x-portable-pixmap", "ppm");
        MIME_SUFFIX.put("image/x-rgb", "rgb");
        MIME_SUFFIX.put("image/x-xbitmap", "xbm");
        MIME_SUFFIX.put("image/x-xpixmap", "xpm");
        MIME_SUFFIX.put("image/x-xwindowdump", "xwd");
        MIME_SUFFIX.put("message/rfc822", "mht");
        MIME_SUFFIX.put("text/css", "css");
        MIME_SUFFIX.put("text/h323", "323");
        MIME_SUFFIX.put("text/html", "html");
        MIME_SUFFIX.put("text/iuls", "uls");
        MIME_SUFFIX.put("text/plain", "txt");
        MIME_SUFFIX.put("text/richtext", "rtx");
        MIME_SUFFIX.put("text/scriptlet", "sct");
        MIME_SUFFIX.put("text/tab-separated-values", "tsv");
        MIME_SUFFIX.put("text/webviewhtml", "htt");
        MIME_SUFFIX.put("text/x-component", "htc");
        MIME_SUFFIX.put("text/x-setext", "etx");
        MIME_SUFFIX.put("text/x-vcard", "vcf");
        MIME_SUFFIX.put("text/xml", "xml");
		MIME_SUFFIX.put("video/3gpp", "3gp");
        MIME_SUFFIX.put("video/mpeg", "mpeg");
        MIME_SUFFIX.put("video/quicktime", "mov");
		MIME_SUFFIX.put("video/mp4", "mp4");
		MIME_SUFFIX.put("video/x-m4v", "m4v");
        MIME_SUFFIX.put("video/x-la-asf", "lsf");
        MIME_SUFFIX.put("video/x-ms-asf", "asf");
        MIME_SUFFIX.put("video/x-msvideo", "avi");
        MIME_SUFFIX.put("video/x-sgi-movie", "movie");
        MIME_SUFFIX.put("x-world/x-vrml", "vrml");
    }

    /**
     * Get MIME type by pass full file name or file suffix name
     * @param suffix File name or file suffix name
     * @return MIME type
     */
    public static String getFileMime(String suffix) {
        if (suffix == null)
            return UNKNOWN_MIME;
        int dotPos = suffix.lastIndexOf('.');
        if (dotPos >= 0)
            suffix = suffix.substring(dotPos+1);
        String mime = MIME.get(suffix.toLowerCase());
        if (mime == null)
            return UNKNOWN_MIME;
        else
            return mime;
    }

    /**
     * Get suffix name(without dot) by MIME type
     * @param mime Mime type
     * @return suffix name
     */
    public static String getFileSuffix(String mime) {
        if (mime == null)
            return "data";
		mime = mime.split(";")[0].trim();
        String suffix = MIME_SUFFIX.get(mime.toLowerCase());
        if (suffix == null)
            return "data";
        else
            return suffix;
    }
}
